﻿using Server.Attributes;
using Server.Message;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Server.Net
{
    [System]
    class NetSystem
    {
        TCPSocket server;

        void Init()
        {

        }

        void Start()
        {
            MySystem.GetSystem<MsgSystem>().Send(Msg.GetRegMsg(MsgType.SettingResponse, InitNet), MsgPriority.Hight);
            MySystem.GetSystem<MsgSystem>().Send(Msg.GetMsg(MsgType.SettingRequire, SettingSystem.NetSet));
        }

        void Update()
        {
            while (!server.RecvMsg.IsEmpty)
            {
                if (server.RecvMsg.TryDequeue(out RecvArgs args))
                {
                    MySystem.GetSystem<MsgSystem>().Send(Msg.GetMsg(MsgType.RPCRequire, args));
                }
            }
        }

        void InitNet(object setting)
        {
            if (setting is not NetSetting set)
                return;

            server = new(set);
            server.Start();
        }

    }
}
